﻿using System.Security.Principal;
using BLL.Security;

namespace Web.Infrastructure.Security
{
    /// <summary>
    /// Helper methods for multilevel authentication.
    /// </summary>
    public static class PrincipalExtensions
    {
        /// <summary>
        /// Gets the administrator identity assiciated with the specified principal.
        /// </summary>
        /// <param name="principal">The principal whose identity to return.</param>
        public static IIdentity GetAdministratorIdentity(this IPrincipal principal)
        {
            if (principal is MultilevelPrincipal)
            {
                return ((MultilevelPrincipal)principal).AdministratorIdentity;
            }

            return new WebIdentity(UserTypes.Anonymous.ToString(), false);
        }

        /// <summary>
        /// Gets the system identity assiciated with the specified principal.
        /// </summary>
        /// <param name="principal">The principal whose identity to return.</param>
        public static IIdentity GetSystemIdentity(this IPrincipal principal)
        {

            if (principal is MultilevelPrincipal)
                return ((MultilevelPrincipal)principal).SystemIdentity;

            return new WebIdentity(UserTypes.Anonymous.ToString(), false);
        }
    }
}